// pages/gjIndex/inspection/inspection.js
import config from "../../../config.js";
var util = require('../../../utils/util.js');
const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    car: null,
    user: null,
    typeList: [],
    typeIndex: 0,
    backAddress: "",
    getAddress: "",
    orderType: 0,
    latitude: 0,
    longitude: 0,
    isPeccancy: 0,
    isRefit: 0,
    serviceType: 2,
    date: util.getDate(),
    time: "00:00",
    // 是否上牌
    hasNo: true,
    tel: "",
    userId: null,
    orderId: -1,
    order: null,
    canChange: 1
  },

  dateChange: function(e) {
    this.setData({
      date: e.detail.value,
    })
  },

  timeChange: function(e) {
    this.setData({
      time: e.detail.value,
    })
  },


  getTypeList: function() {

  },
  submit: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var orderType = 9;
    var orderFrom = 2;
    var addressReturn = that.data.getAddress;
    if (that.data.getAddress == "") {
      wx.showToast({
        title: "请输入服务地址",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    if (that.data.tel == "") {
      wx.showToast({
        title: "请输入联系电话",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    var time = that.data.date;
    wx.request({
      url: openIdUrl.yearSubbmit,
      data: {
        userId: this.data.user.aid,
        houseKeeperId: wx.getStorageSync("houseKeeperId"),
        orderId: that.data.orderId,
        orderType: orderType,
        serviceType: that.data.serviceType,
        carId: this.data.car.cid,
        peccancy: that.data.isPeccancy,
        modify: that.data.isRefit,
        pickTime: that.data.date,
        orderFrom: orderFrom,
        address: addressReturn,
        tel: that.data.tel,
        name: that.data.name,
        time: time,
        locLatitude: that.data.latitude,
        locLongitude: that.data.longitude

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          wx.reLaunch({
            url: "../gjorder/gjorder",
          })
          wx.showLoading({
            title: '订单更改成功！',
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })

  },
  getCarList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.yearAuditInfo,
      data: {
        // id: 1,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            carList: res.data.data
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },

  carChange: function(e) {
    this.setData({
      carIndex: e.detail.value,
    })
    console.log(this.data.carList[this.data.carIndex].cid);
    console.log(this.data.carList[this.data.carIndex].carNo);
  },

  typeChange: function(e) {
    this.setData({
      typeIndex: e.detail.value,
    })
  },

  servChange: function(e) {
    this.setData({
      servIndex: e.detail.value,
    })
  },

  telChange: function(e) {
    this.setData({
      tel: e.detail.value,
    })
  },
  getChange: function(e) {
    this.setData({
      getAddress: e.detail.value,
    })
  },

  selectNo: function() {
    let flag = this.data.hasNo;
    if (flag) {
      this.setData({
        hasNo: false
      })
    } else {
      this.setData({
        hasNo: true
      })
    }
  },


  selectDate: function(e) {
    this.setData({
      date: e.detail.value
    })
  },


  selectAdd: function(e) {
    this.setData({
      addIndex: e.detail.value
    })
  },

  makeCall: function() {
    wx.makePhoneCall({
      phoneNumber: '',
    })
  },
  exchangeCar: function(e) {
    var userId = e.currentTarget.dataset.userid;
    if (userId) {
      wx.navigateTo({
        url: '../carList/carList?userId=' + userId,
      })
    }
  },
  exchangeCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var canChange = options.canChange;
    that.setData({
      canChange: canChange,
      tel: wx.getStorageSync("user").tel,
    })

    //判断是否是再次下单或者更改订单类型
    var orderId = options.orderId;
    var userId = options.uid;

    console.log(orderId);
    console.log(userId);
    if (orderId != null) {
      that.setData({
        orderId: orderId
      })

      //获取订单详情
      that.orderDetail();
    }


  },


  orderDetail() {
    var that = this;
    //获取订单详情
    wx.request({
      url: openIdUrl.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId,
        isKeeper: 1
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data,
            user: res.data.data.order.account
          })
          that.setOrderData();
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  setOrderData() {
    var that = this;
    //订单信息
    var order = that.data.order;
    console.log(order);
    // var service = that.data.order.service;
    that.setData({
      // typeIndex: order.service.insuranceCom.insuranceId - 1,
      user: order.order.account,
      car: order.order.cars,
      getAddress: order.service.location.name,
      tel: order.service.tel,
      isPeccancy: order.service.isPeccancy,
      isRefit: order.service.isRefit,
      serviceType: order.service.yearInspectionType.typeId,

    })
    console.log(that.data.typeIndex);
    console.log(that.data.user);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var item = wx.getStorageSync("userchoose");
    console.log(item);
    if (item != null) {
      this.setData({
        user: item
      })
    }

    var car = wx.getStorageSync("carchoose");
    if (car != null) {
      this.setData({
        car: car
      })

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("carchoose", null);
    wx.setStorageSync("userchoose", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  chooseMap: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          getAddress: res.address
        })
      },
    })
  },
  chooseMap2: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          backAddress: res.address
        })
      }
    })
  },
  peccancyChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      isPeccancy: e.detail.value
    })
  },
  refitChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      isRefit: e.detail.value
    })
  },
  serviceTypeChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      serviceType: e.detail.value
    })
  },
  selectCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },
  exchangeCar: function(e) {
    var id = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../carList/carList?userId=' + id,
    })
  }
})